package cn.portx.report;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import com.bstek.ureport.exception.ReportException;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;

@Component
public class ClassReportProvider implements ReportProvider, ApplicationContextAware {

	private ApplicationContext applicationContext;
	private String prefix = "classpath:ureport/";

	@Override
	public InputStream loadReport(String file) {
		if (!file.startsWith(prefix)) {
			file = prefix + file;
		}
		Resource resource = applicationContext.getResource(file);
		try {
			return resource.getInputStream();
		} catch (IOException e) {
			throw new ReportException(e);
		}
	}

	@Override
	public String getPrefix() {
		return prefix;
	}

	@Override
	public void deleteReport(String file) {
		if (!file.startsWith(prefix)) {
			file = prefix + file;
		}
		try {
			File f = ResourceUtils.getFile(file);
			if (f.exists()) {
				f.delete();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void saveReport(String file, String content) {
		if (!file.startsWith(prefix)) {
			file = prefix + file;
		}
		FileOutputStream outStream = null;
		try {
			outStream = new FileOutputStream(ResourceUtils.getFile(file));
			IOUtils.write(content, outStream, "utf-8");
		} catch (Exception ex) {
			throw new ReportException(ex);
		} finally {
			if (outStream != null) {
				try {
					outStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public List<ReportFile> getReportFiles() {
		try {
			File file = ResourceUtils.getFile(prefix);
			List<ReportFile> list = new ArrayList<ReportFile>();
			for (File f : file.listFiles()) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(f.lastModified());
				list.add(new ReportFile(f.getName(), calendar.getTime()));
			}
			Collections.sort(list, new Comparator<ReportFile>() {
				@Override
				public int compare(ReportFile f1, ReportFile f2) {
					return f2.getUpdateDate().compareTo(f1.getUpdateDate());
				}
			});
			return list;
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean disabled() {
		return false;
	}

	@Override
	public String getName() {
		return "系统内UReport目录";
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
}
